//
//  HWGameLiveRankListCenter.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/11/17.
//

import UIKit
import JXSegmentedView
class HWGameLiveRankListCenter: HWBaseViewController {
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    var httpManager:HWGameLiveHttpManager!
    /// 贡献榜背景
    var contributeBgView:UIView!
    /// 周贡榜
    var weekContributeButton:UIButton!
    /// 月贡榜
    var monthContributeButton:UIButton!
    let weekRankList = HWGameLiveRankListController()
    let monthRankList = HWGameLiveRankListController()
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        
        initFrame()
        
    }
    
}

extension HWGameLiveRankListCenter {
    private func initUI() -> Void {
        
        navigationView.isHidden = true
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["周贡榜","月贡榜"]
        segmentedView.dataSource = segmentedDataSource
        segmentedView.delegate = self
        view.addSubview(self.segmentedView)
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
        contributeBgView = UIView()
        contributeBgView.layer.cornerRadius = 15*widthScale
        contributeBgView.layer.borderWidth = 1
        contributeBgView.layer.borderColor = UIColor.colorWithHexString(hexString: "#CFCFCF").cgColor
        contributeBgView.clipsToBounds = true
        view.addSubview(contributeBgView)
        
        weekContributeButton = UIButton(type: .custom)
        weekContributeButton.backgroundColor = .colorWithHexString(hexString: "#F77105")
        weekContributeButton.setTitle("周贡榜", for: .normal)
        weekContributeButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        weekContributeButton.addTarget(self, action: #selector(weekContributeButtonClick(_:)), for: .touchUpInside)
        contributeBgView.addSubview(weekContributeButton)
        
        monthContributeButton = UIButton(type: .custom)
        monthContributeButton.backgroundColor = .clear
        monthContributeButton.setTitle("月贡榜", for: .normal)
        monthContributeButton.setTitleColor(.colorWithHexString(hexString: "#666666"), for: .normal)
        monthContributeButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        monthContributeButton.addTarget(self, action: #selector(monthContributeButtonClick(_:)), for: .touchUpInside)
        contributeBgView.addSubview(monthContributeButton)
        
        

    }
    
    private func initFrame() -> Void {

        contributeBgView.snp.makeConstraints { make in
            make.width.equalTo(174*widthScale)
            make.height.equalTo(28*widthScale)
            make.centerX.equalToSuperview()
            make.top.equalTo(10)
        }
        
        weekContributeButton.snp.makeConstraints { make in
            make.width.equalTo(174*widthScale/2)
            make.height.equalTo(28*widthScale)
            make.left.equalTo(0)
        }
        
        monthContributeButton.snp.makeConstraints { make in
            make.width.equalTo(174*widthScale/2)
            make.height.equalTo(28*widthScale)
            make.right.equalTo(0)
        }
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().offset(0)
            make.top.equalTo(contributeBgView.snp.bottom).offset(5)
            make.bottom.equalTo(0)
        }
    }
}

extension HWGameLiveRankListCenter: JXSegmentedListContainerViewDataSource{
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return 2
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0  {
            selectWeek()
            weekRankList.type = index
            return weekRankList
        }else {
            selectMonth()
            monthRankList.type = index
            return monthRankList
        }
        
    }
    
    
}

extension HWGameLiveRankListCenter:JXSegmentedListContainerViewListDelegate,JXSegmentedViewDelegate {
    func listView() -> UIView {
        return view
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {
        
        if index == 0 {
            weekRankList.type = index
            selectWeek()
        }else{
            monthRankList.type = index
            selectMonth()
        }
    }
    
}

extension HWGameLiveRankListCenter {
    @objc func weekContributeButtonClick(_ button:UIButton) -> Void {
        selectWeek()
        segmentedView.selectItemAt(index: 0)
    }
    
    @objc func monthContributeButtonClick(_ button:UIButton) -> Void {
        selectMonth()
        segmentedView.selectItemAt(index: 1)
    }
    
    func selectWeek() -> Void {
        weekContributeButton.setTitleColor(.white, for: .normal)
        weekContributeButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        weekContributeButton.backgroundColor = .colorWithHexString(hexString: "#F77105")
        
        monthContributeButton.setTitleColor(.colorWithHexString(hexString: "#666666"), for: .normal)
        monthContributeButton.backgroundColor = .clear
        monthContributeButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
    }
    
    func selectMonth() -> Void {
        monthContributeButton.setTitleColor(.white, for: .normal)
        monthContributeButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        monthContributeButton.backgroundColor = .colorWithHexString(hexString: "#F77105")
        
        weekContributeButton.setTitleColor(.colorWithHexString(hexString: "#666666"), for: .normal)
        weekContributeButton.backgroundColor = .clear
        weekContributeButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
    }
}


